import { getExamDetail, getQuestionDetail, getQuestionRecordAdd, getExamHand } from '@/service/exam.js'
import { defineStore } from 'pinia'
import { useTimer } from './timer'

export const useExam = defineStore('exam', {
  state: () => ({
    examInfo: {
      examid: 0,
      paperid: 0,
      subject: 0,
      type1: '',
      type2: '',
      type3: '',
      section: [],
      time: 0,          //时长
      num: '',           //题量
      title: '',         //title
      labelid: '',       //分类id
    },
    currQuestion: {
      index: -1,            //试题下标
      sectionIndex: -1,     //章节下标
      analysis_gpt: '',     //AI解析
      analysis: '',         //解析
      showAnswer: false,    //展示解析
      videoStatus: false,   //展示视频
    },
  }),
  actions: {
    // 试题变换操作
    async changeQuestion() {
      // 切换试题时， 试题计时器重新计算
      useTimer().quesTimeStart()

      let nextObj = this.examInfo.section[this.currQuestion.sectionIndex].question[this.currQuestion.index]

      const data = {
        examid: this.examInfo.examid,
        paperid: this.examInfo.paperid,
        qid: nextObj.id,
        subject: this.examInfo.subject,
      }
      const quesRes = await getQuestionDetail(data)
      if (quesRes.code == '000000') {

        this.currQuestion = {
          ...nextObj,
          index: this.currQuestion.index,
          sectionIndex: this.currQuestion.sectionIndex,
          ...quesRes.data,
        }
      }
    },

    // 提交试题操作
    async submitQuestion() {
      // 选项未选择 return 直接切换下一题不做提交试题操作 / 单选/多选
      if (this.currQuestion.type == 1 || this.currQuestion.type == 3) {
        if (this.currQuestion?.answerList?.findIndex((it) => it.is_check == 1) == -1) {
          return
        }
      }

      // 答题内容为空 return 直接切换下一题不做提交试题操作 / 填空
      if (this.currQuestion.type == 4 || this.currQuestion.type == 5 || this.currQuestion.type == 8) {
        if (!this.currQuestion.answer_content || this.currQuestion.answer_content == '<p><br></p>') {
          return
        }
      }

      if(this.currQuestion.type == 9 || this.currQuestion.type == 10){
        if(!this.currQuestion.child.length){
          return 
        }
      }

      const data = {
        examid: this.examInfo.examid,
        paperid: this.examInfo.paperid,
        question_time: useTimer().quesTime,
        time: useTimer().paperTime,
        questionid2: this.currQuestion?.id,
        sectionid: this.examInfo.section[this.currQuestion.sectionIndex].sectionid,
        subject: this.examInfo.subject,
        type1: this.examInfo.type1,
        type2: this.examInfo.type2,
        type3: this.examInfo.type3,
        is_right: this.currQuestion?.is_right || 0,
        answer: this.currQuestion?.answer || '',
        answer_content: this.currQuestion?.answer_content || '',
        child: this.currQuestion?.child || [],
      }

      const res = await getQuestionRecordAdd(data)

      return res
    },

    // 提交试卷操作
    async submitPaper() {
      // 选项选择后先提交当前试题  再提交试卷
      if (this.currQuestion?.answerList?.findIndex((it) => it.is_check == 1) !== -1) {
        await this.submitQuestion()
      }

      const data = {
        examid: this.examInfo.examid,
        paperid: this.examInfo.paperid,
      }

      const res = await getExamHand(data)

      return res
    },

    // 初始化考试信息数据、试题信息数据
    getInit(){
      this.currQuestion = {},
      this.examInfo = {
        examid: 0,
        paperid: 0,
        subject: 0,
        type1: this.examInfo.type1,
        type2: this.examInfo.type2,
        type3: this.examInfo.type3,
        section: [],
        time: 30,          //时长
        num: 10,           //题量
        title: '',         //title
        labelid: 0,       //分类id
      }
    }
  },
  persist: {
    storage: piniaPluginPersistedstate.localStorage(),
  },
})
